package org.freedom.railhunter.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.jna.Library;
import com.sun.jna.Native;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import org.freedom.railhunter.service.TicketService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UUWiseHelper {
    
    private static final Logger logger = LoggerFactory.getLogger(TicketService.class);
    
    /**
     * 初始化（设置和登录）
     * 
     * @return
     * @throws IOException 
     */
    public static void initAndLogin(String userName, String password) throws IOException {
        UUAPI.SOFTID = 95191;
        UUAPI.SOFTKEY = "bc908c2b604d4c38b18792d374fc2d64";	//KEY 获取方式：http://dll.uuwise.com/index.php?n=ApiDoc.GetSoftIDandKEY
        UUAPI.DLLVerifyKey = "9890400A-33E3-493A-A53E-CB14DA74FD6A";//校验API文件是否被篡改，实际上此值不参与传输，关系软件安全，高手请实现复杂的方法来隐藏此值，防止反编译,获取方式也是在后台获取软件ID和KEY一个地方

        UUAPI.USERNAME = userName;		//用户帐号和密码(非开发者帐号)，在打码之前，需要先设置好，给用户留一个输入帐号和密码的地方
        UUAPI.PASSWORD = password;
        
        if (!UUAPI.checkAPI()) {
            throw new IllegalArgumentException("Login UUWise failed.");
        }
    }

    /**
     * 识别
     * 
     * @param imageBytes
     * @return
     * @throws IOException 
     */
    public static String recognizeRandCode(byte[] imageBytes) {
        try {
            String[] result = UUAPI.easyDecaptcha(imageBytes, 1004);
            logger.debug("this img codeID:" + result[0]);
            logger.debug("return recongize Result:" + result[0]);
            return result[1];
        } catch (UnsupportedEncodingException ex) {
            logger.error("", ex);
            return "";
        } catch (IOException ex) {
            logger.error("", ex);
            return "";
        }
    }
}